package com.kcht.sylyg08.service;

import com.kcht.adp.common.log.Logger;
import com.kcht.adp.pagination.Page;
import com.kcht.sylyg08.dao.IAntDao;
import com.kcht.sylyg08.dao.IReaderDao;
import com.kcth.sylyg08.entity.Ant;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

/**
 * @program: G08ProductionLine
 * @description
 * @author: X.Gao
 * @create: 2019-07-25 08:53
 **/


@RequestScoped
@Transactional()
public class AntService {

    @Inject
    private Logger logger;

    @EJB
    private IAntDao antDao;

    @EJB
    private IReaderDao readerDao;

    //分页查询信息
    public Page<Ant, Ant> list(Page<Ant, Ant> page) {

        return antDao.getPageListByJPQL("Ant.queryRecordList", page);

    }

    //如果没有发现对应的阅读器IP，则不进行存储
    public Boolean save(Ant ant) {
        if (ant.getId() == null) {
            if (null != readerDao.getReaderByIP(ant.getIp())) {
                ant.setReaderId(readerDao.getReaderByIP(ant.getIp()).getId());
                antDao.save(ant);
                return true;
            }
            return false;
        } else {
            antDao.merge(ant);
            return true;
        }
    }

    //删除记录
    public void delete(List<Integer> ids) {
        ids.forEach(id -> {
            Ant ant = antDao.get(id.longValue());
            if (Objects.nonNull(ant)) {
                antDao.delete(id.longValue());
            }
        });
    }

}
